#ifndef __ESP32_BLUETOOTH
#define __ESP32_BLUETOOTH

#include <stdint.h>
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_main.h"
#include "gatts_table_creat_demo.h"
#include "esp_gatt_common_api.h"
#define PROFILE_NUM                 1
#define PROFILE_APP_IDX             0
#define ESP_APP_ID                  0x55
#define SAMPLE_DEVICE_NAME          "ESP_GATTS_DEMO"
#define SVC_INST_ID                 0

#define BLE_TAG "BLUETOOTH"

//在这里修改选择是否使用HX711
#define USE_HX711

/* The max length of characteristic value. When the GATT client performs a write or prepare write operation,
*  the data length must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
*/
#define GATTS_DEMO_CHAR_VAL_LEN_MAX 500
#define PREPARE_BUF_MAX_SIZE        1024
#define CHAR_DECLARATION_SIZE       (sizeof(uint8_t))

#define ADV_CONFIG_FLAG             (1 << 0)
#define SCAN_RSP_CONFIG_FLAG        (1 << 1)

extern uint8_t adv_config_done;

typedef struct {
    uint8_t                 *prepare_buf;
    int                     prepare_len;
} prepare_type_env_t;

extern  prepare_type_env_t prepare_write_env;//

extern uint16_t heart_rate_handle_table[HRS_IDX_NB];

extern bool notify_flag;
extern int send_count;

#define CONFIG_SET_RAW_ADV_DATA
#ifdef CONFIG_SET_RAW_ADV_DATA
extern uint8_t raw_adv_data[];
extern uint8_t raw_scan_rsp_data[];

#endif /* CONFIG_SET_RAW_ADV_DATA */

extern esp_ble_adv_params_t adv_params;

struct gatts_profile_inst {
    esp_gatts_cb_t gatts_cb;
    uint16_t gatts_if;
    uint16_t app_id;
    uint16_t conn_id;
    uint16_t service_handle;
    esp_gatt_srvc_id_t service_id;
    uint16_t char_handle;
    esp_bt_uuid_t char_uuid;
    esp_gatt_perm_t perm;
    esp_gatt_char_prop_t property;
    uint16_t descr_handle;
    esp_bt_uuid_t descr_uuid;
};

void gatts_profile_event_handler(esp_gatts_cb_event_t event,
					esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);//

/* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
extern struct gatts_profile_inst heart_rate_profile_tab[PROFILE_NUM];

/* Service */
extern const uint16_t GATTS_SERVICE_UUID_TEST;//
extern const uint16_t GATTS_CHAR_UUID_TEST_A;//


extern const uint16_t primary_service_uuid;//
extern const uint16_t character_declaration_uuid;//
extern const uint16_t character_client_config_uuid;//

extern const uint8_t char_prop_read_write_notify ;//
extern const uint8_t heart_measurement_ccc[2];//
extern const uint8_t char_value[10];//



/* Full Database Description - Used to add attributes into the database */
extern const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB];

void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);//
void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
// static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);//
void ble_init();

#endif